//
//  OADLayout6ViewController.m
//  OASDK_Works
//
//  Created by ChenJie on 4/7/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "OADLayout6ViewController.h"
#import "OALabel.h"
#import "OABase.h"
#import "OADMocker.h"
#import "UIView+OADLayout6.h"
#import "OADLayoutControlPanel.h"

@interface OADLayout6ViewController ()

@property (nonatomic, strong) OALabel *label;

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) OADLayoutControlPanel *controlPanel;
@property (nonatomic, strong) UIView *container;
@property (nonatomic, strong) UIView *view1;
@property (nonatomic, strong) UIView *view11;
@property (nonatomic, strong) UIView *view111;
@property (nonatomic, strong) UIView *view112;
@property (nonatomic, strong) UIView *view12;
@property (nonatomic, strong) UIView *view2;
@property (nonatomic, strong) UIView *view3;
@property (nonatomic, strong) UIView *view31;

@end

@implementation OADLayout6ViewController

#pragma mark - ViewController lifecycle

- (instancetype)init {
    self = [super init];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = NSStringFromClass(self.class);
    
    //
//    [self.view addSubview:self.label];
//    self.label.text = [OADMocker mockTextWithMinLength:100 maxLength:200];
    
    OADLayout6 *containerLayout = [OADLayout6 new];
    containerLayout.flexDirection = OADFlexDirectionRow;
    containerLayout.margin = 10;
//    containerLayout.padding = 10;
    
    containerLayout.width = 350;
    containerLayout.height = 400;
    
    self.container.oa_layout6 = containerLayout;
    
    
    //
    OADLayout6 *layout1 = [OADLayout6 new];
    layout1.flexDirection = OADFlexDirectionColumn;
    layout1.justifyContent = OADJustifyFlexStart;
    layout1.alignItems = OADAlignFlexStart;
    
    layout1.marginLeft = 10;
    layout1.marginTop = 5;
    layout1.marginRight = 0;
    layout1.marginBottom = 5;
    
//    layout1.padding = 10;
    
//    layout1.width = 30;
//    layout1.height = 40;
    
    layout1.flexGrow = 1;
    
    self.view1.oa_layout6 = layout1;
    
    //
    OADLayout6 *layout11 = [OADLayout6 new];
    
    layout11.flexDirection = OADFlexDirectionColumn;
    layout11.justifyContent = OADJustifyFlexStart;
    layout11.alignItems = OADAlignFlexStart;
    
    layout11.margin = 5;
    
//    layout11.padding = 10;
    
//    layout11.width = 50;
//    layout11.height = 50;
    
    layout11.flexGrow = 1;
    
    self.view11.oa_layout6 = layout11;
    
    //
    OADLayout6 *layout111 = [OADLayout6 new];
    
    layout111.marginLeft = 10;
    layout111.marginTop = 15;
    layout111.marginRight = 20;
    layout111.marginBottom = 25;
    
    layout111.width = 100;
    layout111.height = 50;
    
    layout111.flexGrow = 1;
    
    self.view111.oa_layout6 = layout111;
    
    
    //
    OADLayout6 *layout112 = [OADLayout6 new];
    layout112.padding = 5;
    
    layout112.width = 50;
    layout112.height = 50;
    
//    layout112.flexGrow = 1;
    
    self.view112.oa_layout6 = layout112;
    
    
    //
    OADLayout6 *layout12 = [OADLayout6 new];
    
    layout12.margin = 10;
    
    layout12.width = 40;
    layout12.height = 50;
    
//    layout12.flexGrow = 1;
    
    self.view12.oa_layout6 = layout12;
    
    
    //
    OADLayout6 *layout2 = [OADLayout6 new];
    
    layout2.marginLeft = 10;
    layout2.marginTop = 5;
    layout2.marginRight = 10;
    layout2.marginBottom = 5;
    
    layout2.width = 40;
    layout2.height = 50;
    
//    layout2.flexGrow = 1;
    
    self.view2.oa_layout6 = layout2;
    
    
    //
    OADLayout6 *layout3 = [OADLayout6 new];
    layout3.margin = 5;
    
//    layout3.width = 50;
//    layout3.height = 60;
    layout3.flexGrow = 1;
    
    self.view3.oa_layout6 = layout3;
    
    //
    OADLayout6 *layout31 = [OADLayout6 new];
    layout31.marginLeft = 15;
    layout31.marginTop = 5;
    layout31.marginRight = 5;
    layout31.marginBottom = 15;
    
    layout31.width = 50;
    layout31.height = 50;
    layout31.flexGrow = 1;
    
    self.view31.oa_layout6 = layout31;
    
    
    
    //
    [self.view addSubview:self.contentView];
    [self.view addSubview:self.controlPanel];
    [self.contentView addSubview:self.container];
    
    
    [self.container addSubview:self.view1];
    [self.view1 addSubview:self.view11];
    [self.view11 addSubview:self.view111];
//    [self.view11 addSubview:self.view112];
    [self.view1 addSubview:self.view12];
    
    [self.container addSubview:self.view2];
    
//    [self.container addSubview:self.view3];
//    [self.view3 addSubview:self.view31];
    
    self.view.clipsToBounds = YES;
    self.contentView.clipsToBounds = YES;
    self.container.clipsToBounds = YES;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
}

#define kMarginH 10
#define kMarginTop 15
#define kLabelFont [UIFont systemFontOfSize:16]

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    //
    CGFloat topMark = 0;
    CGFloat controlPanelTopMargin = 0;
    CGFloat controlPanelHeight = [OADLayoutControlPanel controlPanelHeight];
    
    //
    CGFloat controlPanelTop = self.view.oa_height - controlPanelHeight;
    self.controlPanel.frame = CGRectMake(0, controlPanelTop, self.view.oa_width, controlPanelHeight);
    
    //
    topMark = OA_NAVIGATION_BAR_BOTTOM;
    CGFloat contentHeight = controlPanelTop - controlPanelTopMargin - topMark;
    self.contentView.frame = CGRectMake(0, topMark, self.view.oa_width, contentHeight);
    
    
    [self.container.oa_layout6 applyLayout];
    [self.container oa_showBoundsWithAlpha:0.5];
    
    
//    CGFloat labelWidth = OA_SCREEN_WIDTH - kMarginH * 2;
//    CGSize labelSize = [self.label.text oa_sizeWithWidth:labelWidth font:kLabelFont padding:self.label.contentInsets];
//    self.label.frame = CGRectMake(kMarginH, topMark, labelSize.width, labelSize.height);
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

#pragma mark - Public methods



#pragma mark - Private methods

- (void)updateLayoutWithDirection:(Direction)direction
                   justifyContent:(JustifyContent)justifyContent
                       alignItems:(AlignItems)alignItems {
    
    self.container.oa_layout6 = [OADLayout6 new];
    self.container.oa_layout6.margin = 15;
    self.container.oa_layout6.width = 350;
    self.container.oa_layout6.height = 300;
    
    self.container.oa_layout6.flexDirection = (NSInteger)direction;
    self.container.oa_layout6.justifyContent = (NSInteger)justifyContent;
    self.container.oa_layout6.alignItems = (NSInteger)alignItems;
    
    [self.container.oa_layout6 applyLayout];
}

#pragma mark - Event response methods

- (void)buttonClicked:(UIButton *)button {
    NSLog(@"button clicked...");
}

#pragma mark - Deletate methods



#pragma mark - Setter



#pragma mark - Getter

- (OALabel *)label {
    if (!_label) {
        OALabel *label = [[OALabel alloc] initWithFrame:CGRectZero];
        
        label.font = kLabelFont;
        label.textColor = OARandomColor();
        label.backgroundColor = OARandomColor();
        label.contentInsets = UIEdgeInsetsMake(5, 5, 5, 5);
        label.numberOfLines = 0;
        
        _label = label;
    }
    return _label;
}

OAGetView(contentView, ^(UIView *view){
    
})

- (OADLayoutControlPanel *)controlPanel {
    if (!_controlPanel) {
        OADLayoutControlPanel *panel = [OADLayoutControlPanel controlPanelWithDirection:(NSInteger)self.container.oa_layout6.flexDirection
                                                                         justifyContent:(NSInteger)self.container.oa_layout6.justifyContent
                                                                             alignItems:(NSInteger)self.container.oa_layout6.alignItems];
        __weak typeof(self) weakSelf = self;
        panel.layoutDidChange = ^(Direction direction, JustifyContent justifyContent, AlignItems alignItems) {
            [weakSelf updateLayoutWithDirection:direction justifyContent:justifyContent alignItems:alignItems];
        };
        _controlPanel = panel;
    }
    return _controlPanel;
}

OAGetView(container, ^(UIView *view){
    view.backgroundColor = OARandomColor();
})

OAGetView(view1, ^(UIView *view){
    view.backgroundColor = OARandomColor();
    
})

OAGetView(view11, ^(UIView *view){
    view.backgroundColor = OARandomColor();
    
})

OAGetView(view111, ^(UIView *view){
    view.backgroundColor = OARandomColor();
    
})

OAGetView(view112, ^(UIView *view){
    view.backgroundColor = OARandomColor();
    
})

OAGetView(view12, ^(UIView *view){
    view.backgroundColor = OARandomColor();
    
})

OAGetView(view2, ^(UIView *view){
    view.backgroundColor = OARandomColor();
    
})

OAGetView(view3, ^(UIView *view){
    view.backgroundColor = OARandomColor();
})

OAGetView(view31, ^(UIView *view){
    view.backgroundColor = OARandomColor();
})


#pragma mark - Memory management

- (void)dealloc {
}

@end
